package com.aiti.lulian.controller.newCard;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.newCard.ExchangeCardParamDto;
import com.aiti.lulian.dto.newCard.MyCardHolderParamDto;
import com.aiti.lulian.dto.newCard.MyExchangeRequestPageParamDto;
import com.aiti.lulian.service.newCard.CardExchangeService;
import com.aiti.lulian.vo.newCard.ExchangeCardListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * description:
 * auth fcb
 * date 2025-08-13 17:24
 **/
@RestController
@RequestMapping("/new-card/exchange")
@Slf4j
@Api(tags = "名片交换控制层")
public class CardExchangeController {

    @Autowired
    private CardExchangeService cardExchangeService;

    @GetMapping("/my-card-list")
    @ApiOperation("交换名片时查询我的名片列表")
    public Message queryExchangeCardList(@RequestParam("userId") String userId) {
        try {
            return Message.success(cardExchangeService.queryExchangeCardList(userId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询名片交换列表异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作失败");
        }
    }

    @PostMapping("/send-exchange-card")
    @ApiOperation("发送名片交换请求")
    public Message exchangeCard(@RequestBody ExchangeCardParamDto paramDto) {
        try {
            cardExchangeService.exchangeCard(paramDto);
            return Message.success();
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("发送名片交换请求异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作失败");
        }
    }

    @GetMapping("/my-exchange-request-msg")
    @ApiOperation("查询我的名片交换请求列表")
    public Message queryMyExchangeRequestList(@RequestParam("userId") String userId) {
        try {
            return Message.success(cardExchangeService.queryMyExchangeRequestList(userId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询名片交换列表异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作失败");
        }
    }

    @PutMapping("/update-exchange-status")
    @ApiOperation("更新名片交换状态")
    public Message updateExchangeStatus(@RequestParam("baseId") String baseId, @RequestParam("enable") String enable) {
        try {
            cardExchangeService.updateExchangeStatus(baseId, enable);
            return Message.success();
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("更新名片交换状态异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作失败");
        }
    }

    @PostMapping("/my-exchange-request-page-list")
    @ApiOperation("查询我的名片交换请求列表")
    public Message queryMyExchangeRequestPageList(@RequestBody MyExchangeRequestPageParamDto paramDto) {
        try {
            return Message.success(cardExchangeService.queryMyExchangeRequestPageList(paramDto));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询名片交换列表异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作失败");
        }
    }

    @GetMapping("/my-exchange-request-count")
    @ApiOperation("查询我的名片交换请求数量")
    public Message queryMyExchangeRequestCount(@RequestParam("userId") String userId) {
        try {
            return Message.success(cardExchangeService.queryMyExchangeRequestCount(userId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询名片交换列表异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作失败");
        }
    }

    @PostMapping("/my-card-holder-list")
    @ApiOperation("查询我的名片持有者列表")
    public Message queryMyCardHolderList(@RequestBody MyCardHolderParamDto paramDto) {
        try {
            return Message.success(cardExchangeService.queryMyCardHolderList(paramDto));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询名片交换列表异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作失败");
        }
    }

    @DeleteMapping("/delete-user-holder-card")
    @ApiOperation("删除用户持有名片")
    public Message deleteUserHolderCard(@RequestParam("fromUserId") String fromUserId, @RequestParam("toCardId") String toCardId) {
        try {
            cardExchangeService.deleteUserHolderCard(fromUserId, toCardId);
            return Message.success();
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("删除用户持有名片异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作失败");
        }
    }


}
